package com.asset.config;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.util.Assert;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;

/**
 * BigDecimal 类 JSON 反序列化
 * @author 11725
 */
public class BigDecimalDeserializer extends JsonDeserializer<BigDecimal> {


    @Override
    public BigDecimal deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        // 保留两位小数
        return Objects.isNull(jsonParser.getDecimalValue()) ? null :
                jsonParser.getDecimalValue().setScale(2, RoundingMode.FLOOR);
    }
}
